import { createRouter, createWebHistory } from 'vue-router'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home',
      component: () => import('@/views/home/index.vue'),
      // 添加字段是否显示底部Tabbar切换
      meta: {
        showTabbar: true
      }
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('@/views/home/index.vue'),
      meta: {
        showTabbar: true
      }
    },
    {
      path: '/follow',
      name: 'follow',
      component: () => import('@/views/follow/index.vue'),
      meta: {
        showTabbar: true
      }
    },
    {
      path: '/message',
      name: 'message',
      component: () => import('@/views/message/index.vue'),
      meta: {
        showTabbar: true
      }
    },
    {
      path: '/my',
      name: 'my',
      component: () => import('@/views/my/index.vue'),
      meta: {
        showTabbar: true
      }
    },
    {
      path: '/message/fan',
      name: 'fan',
      component: () => import('@/views/message/FanComponent/index.vue')
    },
    {
      path: '/message/like',
      name: 'like',
      component: () => import('@/views/message/LikeComponent/index.vue')
    },
    {
      path: '/message/atMy',
      name: 'atMy',
      component: () => import('@/views/message/AtMyComponents/index.vue')
    },
    {
      path: '/message/comment',
      name: 'comment',
      component: () => import('@/views/message/CommentPage/index.vue')
    },
    {
      path: '/message/contact',
      name: 'contact',
      component: () => import('@/views/message/Contact/index.vue')
    },
    {
      path: '/Chat',
      name: 'chat',
      component: () => import('@/views/message/ChatComponent/index.vue')
    },
    {
      path: '/my/EditPersonalInformation',
      name: 'EditPersonalInformation',
      component: () => import('@/views/my/EditPersonalInformation/index.vue')
    },
    {
      path: '/home/SearchPage',
      name: 'SearchPage',
      component: () => import('@/views/home/SearchPage/index.vue')
    },
    {
      path: '/uploadVideo',
      name: 'uploadVideo',
      component: () => import('@/views/uploadVideo/index.vue'),
    },
    {
      path: '/my/Myfollowings',
      name: 'Myfollowings',
      component: () => import('@/views/my/Myfollowings/index.vue')
    },
    {
      path: '/my/MyFans',
      name: 'MyFans',
      component: () => import('@/views/my/MyFans/index.vue')
    },
    // 用户详情页
    {
      path: '/UserDetails/:friendId?',
      name: 'UserDetails',
      component: () => import('@/views/home/FriendsProfilePage/index.vue')
    },
    // TA 关注的页面
    {
      path: '/FriendFollow/:friendId?',
      name: 'FriendFollow',
      component: () => import('@/views/home/FriendsProfilePage/TAsfocus/index.vue')
    },
    // // TA 粉丝的页面
    {
      path: '/Friendsandfans/:friendId?',
      name: 'Friendsandfans',
      component: () => import('@/views/home/FriendsProfilePage/TAsfans/index.vue')
    },
  ],

})

// 跳转的时候判断是否登录  home直接放行 其他的判断是否登录
// 未登录则跳转到登录页面 并更改登录状态为false

import { useUserStore } from '@/stores/userStatus'
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  if (to.name !== 'home' && !userStore.isLogin) {
    // 非首页 + 未登录 → 放行到首页，但打开登录弹窗
    next({ name: 'home' })
    userStore.openLoginModal() // 只是打开弹窗，不改变 isLogin
  } else {
    next()
  }
})



export default router
